package com.share.module.business.echart.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.Quarter;
import com.share.module.business.echart.service.EchartsService;
import com.share.module.business.echart.vo.HomeReqVO;
import com.share.module.business.echart.vo.HomeVO;
import com.share.module.sys.user.entity.User;
import com.share.module.sys.user.service.UserService;
import com.stock.common.response.ApiResult;
import com.stock.common.utils.DateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    private UserService userService;
    @Resource
    private EchartsService echartsService;

    @GetMapping("/home")
    public ApiResult<HomeVO> home(HomeReqVO vo) {
        return ApiResult.success(echartsService.home(vo));
    }

    @GetMapping("/example")
    public ApiResult<Map<String, Object>> get() {
        Map<String, Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("20230331", "20230630", "20230930", "20231231"));
        map.put("y", CollUtil.newArrayList(24.55, 24.55, 21.92, 0));
        return ApiResult.success(map);
    }

    @GetMapping("/members")
    public ApiResult<?> members() {
        List<User> list = userService.list();
        int q1 = 0; // 第一季度
        int q2 = 0; // 第二季度
        int q3 = 0; // 第三季度
        int q4 = 0; // 第四季度

        for (User user : list) {
            Quarter quarter = DateUtils.quarterEnum(DateUtils.asDate(user.getCreateDate()));
            switch (quarter) {
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
                default: break;
            }
        }

        return ApiResult.success(CollUtil.newArrayList(q1, q2, q3, q4));
    }

}
